package com.example.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Leetcode0009 {
    public boolean isPalindrome(int x) {
//        return solution1(x);
        return solution2(x);
    }

    private boolean solution2(int x) {
        if (x < 0) {
            return false;
        }
        int newData = 0;
        int tmp = x;
        while (tmp != 0) {
            int mod = tmp % 10;
            newData = newData * 10 + mod;
            tmp = tmp / 10;
        }
        return newData == x;
    }

    /**
     先不断取模，将每一位数字取出放入arr中，然后从中间开始向两头遍历，判断是否是回文数字
     */
    private boolean solution1(int x) {
        if (x < 0) {
            return false;
        }
        if (x == 0) {
            return true;
        }
        List<Integer> arr = new ArrayList();
        int tmp = x;
        while( tmp != 0) {
            int mod = tmp % 10;
            arr.add(mod);
            tmp = tmp / 10;
        }
        // 是奇数，从中间开始
        int len = arr.size();
        int mid = len / 2;
        int low = mid;
        int high = mid;
        if (len % 2 == 0) {
            // 如果是偶数
            low = mid-1;
            high = mid;
        }

        for(; low >= 0 && high <= len -1; ) {
            if(arr.get(low--).equals(arr.get(high++))){
                continue;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Leetcode0009 leetcode0009 = new Leetcode0009();
        System.out.println(leetcode0009.isPalindrome(121));
    }
}
